import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Deom1 {//sql插入
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入名字");
        int name = scanner.nextInt();
        System.out.println("请输入语文成绩");
        int Chinese = scanner.nextInt();
        System.out.println("请输入英语成绩");
        int English = scanner.nextInt();
        System.out.println("请输入数学成绩");
        int math = scanner.nextInt();
        //1.先创建 DataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("88800662");

        //2. 建立和数据库服务器之间的连接
        Connection connection = dataSource.getConnection();

        //3.构造sql
        //String sql = ("insert into score values(7,56,78,98)");
        //String sql = ("delete from score where math = 98");
        String sql = ("insert into score values(?,?,?,?)");
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
       preparedStatement.setInt(1,name);
        preparedStatement.setInt(2,Chinese);
        preparedStatement.setInt(3,English);
        preparedStatement.setInt(4,math  );


        //4.把sql发给服务器，返回值是一个整数，表示影响到的行数
        int n = preparedStatement.executeUpdate();
        System.out.println(n);

        //5.关闭连接，释放资源(软/硬)，后获得的资源，先释放
        preparedStatement.close();
        connection.close();
    }
}
